home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.11 Nov 96 / Symantec Top Ten Code / MacSerialPort.c next >
Encoding:
C/C++ Source or Header  |  1996-10-24  |  3.7 KB  |  123 lines  |  [TEXT/R*ch]

  1. // MacSerialPort.c
  2. // MacTech's Symantec Top Ten, November 1996
  3. //
  4. // This code sample walks through the basics of sending and receiving 
  5. // character data from a serial port
  6. //
  7. // Thanks to Mark Y. Geschelin for the original code this is based on.
  8. //
  9.  
  10. #include <console.h>
  11. #include <Serial.h>
  12. #include <Devices.h>
  13. #include <stdio.h>
  14. #include <console.h>
  15. #include <stdlib.h>
  16. #include <Serial.h>
  17. #include <Devices.h>
  18.  
  19. #define SERBUFSIZ 1024        // Define the Input buffer size to use
  20.  
  21. char *inbuf;                // pointer to input character buffer
  22. short inRefNum, outRefNum;    // Device driver Reference Number holders
  23.  
  24. /////////////////////////////////////////
  25. //  Initialize the Serial Port   //
  26. /////////////////////////////////////////
  27. OSErr InitializeSerialPort()
  28. {
  29.     OSErr   err;
  30.     SerShk flags;
  31.     Ptr buf;
  32.     
  33.      // Open Serial Drivers (note: Use ".BIn" and ".BOut" for Printer port)
  34.     // assign Output and Input driver reference numbers
  35.     if (err = OpenDriver("\p.AOut",&outRefNum)) return err;
  36.     if (err = OpenDriver("\p.AIn", &inRefNum)) return err;    
  37.     
  38.     // Initialize input and output drivers, and 
  39.     // assign basic communication protocols 
  40.     if (err = SerReset(outRefNum,baud57600 + data8 + stop10+noParity))
  41.         return err;
  42.     if (err = SerReset(inRefNum,baud57600 + data8 + stop10+noParity)) 
  43.         return err;
  44.     
  45.     // Set up the serial input driver to use a buffer of size SERBUFSIZ
  46.     if(!(buf = NewPtr(SERBUFSIZ))) return MemError();
  47.     if (err = SerSetBuf(inRefNum, buf, SERBUFSIZ)) return err;
  48.     
  49.     // Specify handshaking and cotrol info for the input driver 
  50.     flags.fXOn = false;    // XOn/Xoff Output enabled?
  51.     flags.fCTS = true;    // Using Clear To Send harware handshaking?
  52.     flags.xOn = 0x11;        // Character for XOn
  53.     flags.xOff = 0x13;    // Character for XOff
  54.     flags.errs = false;    // Abort Input requests if: Parity error
  55.                         //        or: Hardware overrun
  56.                         //        or: Franing error
  57.     flags.evts = false;    // Post event on CTS or Break status change
  58.     flags.fInX = false;    // XOn/Xoff Input enabled?
  59.     flags.fDTR = false;    // Using Data Terminal Ready flow control
  60.  
  61.     // Set driver to reflect settings
  62.     if ( err = SerHShake(outRefNum,&flags)) return err;    
  63.     // Allocate input buffer; return reason on failure    
  64.     if (!( inbuf = (char *) NewPtr(SERBUFSIZ))) return MemError();    
  65.  
  66.     return noErr;        // noErr = 0
  67. }
  68.  
  69. ////////////////////////////////////////////////////////
  70. //    Send a String to the Serial Port    //
  71. ////////////////////////////////////////////////////////
  72. void SendSerial(char *outString, long strLen)
  73. {
  74.    FSWrite(outRefNum,  &strLen, outString);
  75. }
  76.  
  77. // Thanks to Andrew McFarland, Noah Lieberman and Levi Brown for their contributions. 
  78.  
  79.  
  80. /////////////////////////////
  81. //     Main     //
  82. /////////////////////////////
  83. int main(void)
  84. {
  85.     OSErr err;
  86.     long count;
  87.     char keyChar;
  88.  
  89.     csetmode(C_RAW,stdin);            // disable echo and line buffering for input
  90.  
  91.     if (err = InitializeSerialPort())    // Check for failure to initialize port
  92.         printf("Serial initialization failed. Error = %d\n",err);
  93.     else
  94.     {
  95.         SendSerial("ATX\r",5);            // Send ubiquitous Hayes reset
  96.  
  97.         keyChar = getchar();            // Get a character from stdin
  98.         while (keyChar != 0x1B)        // Loop until escape key is pressed
  99.         {
  100.             if (keyChar > 0)            // Is there a character to send?
  101.             SendSerial(&keyChar,1);    // Call SendSerial to send it.
  102.                
  103.             SerGetBuf(inRefNum, &count);    
  104.                             // Is there anything in the Input buffer
  105.             if (count)                            
  106.             {
  107.                 FSRead(inRefNum,&count,inbuf);    
  108.                             // Read all chars from Input driver
  109.                 // Send to console
  110.                 for (long i=0; i < count; putchar(inbuf[i++]));
  111.             }
  112.             keyChar = getchar();        // Get another character from stdin
  113.         }
  114.     }
  115.     
  116.     // Clean up: Reset Ports, return pointer
  117.     if(outRefNum) CloseDriver(outRefNum);
  118.     if(inRefNum) CloseDriver(inRefNum);
  119.     if (inbuf)  DisposPtr(inbuf);
  120.  
  121.     return EXIT_SUCCESS;
  122. }
  123.